#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import cx_Oracle  #导入模块
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'  # 解决中文乱码问题

###############################################################################
#
# oracle数据库连接及工具的使用
#
# 1. 生成相应的基础代码
###############################################################################


class db_class:
    # 初始化（连接数据库，并获取数据）
    def __init__(self):
        self.tableName = 'WB_TBL_BASE_LOG'
        self.multilingual = 'base.baseLog'

    def db_data(self):
        # 连接数据库
        # 连接user/passwd@host:端口/instance
        conn = cx_Oracle.connect(
            'prm_zm_test1/prm_zm_test1@192.168.101.87:1521/cice')
        # conn = cx_Oracle.connect(connectString, encoding = "UTF-8", nencoding = "UTF-8")             # 解决中文乱码问题
        # 创建游标对象
        c = conn.cursor()
        # 执行命令
        # x = c.execute('select sysdate from dual')
        x = c.execute(
            "SELECT t1.column_name, t1.data_type, t2.comments FROM	user_tab_cols t1 left join user_col_comments t2 ON t1.table_name = t2.table_name 	AND t1.column_name = t2.column_name WHERE	t1.Table_Name = '"
            + self.tableName +
            "' 	AND t1.column_name NOT IN ( 'DELETE_FLAG', 'CREATE_USER', 'CREATE_DATE', 'UPDATE_USER', 'UPDATE_DATE', 'VERSION_NUM', 'ID' )"
        )
        # data = x.fetchone()       # 返回单个元组，如果查询不到结果，则返回None(也就是只返回一条数据)
        # print(data)

        self.data = x.fetchall()  # 返回二维元组，如果查询不到结果，则返回()
        # print(data)
        # 关闭游标对象
        c.close()
        # 关闭连接
        conn.close()

    # 下划线转驼峰
    def str2Hump(self):
        self.arrs = []
        for f in self.data:
            arr = []
            for i in range(len(f)):
                if i == 0:
                    title = f[0].title().replace('_', '')
                    col = title[0].lower() + title[1:]
                    # print(col)
                    arr.append(col)
                else:
                    arr.append(f[i])
            arr.append(title)
            self.arrs.append(arr)

    def do1(self):
        ret = ''
        for f in self.arrs:
            column_name = f[0]
            data_type = f[1]
            comments = f[2]
            if data_type == 'NUMBER':
                ret += '<!-- ' + comments + '  -->\n'
                ret += "<el-table-column align='left' prop='" + column_name + "' :label=\"$t('" + comments + "')\" width='100' show-overflow-tooltip>\n"
                ret += "    <template slot-scope='scope'>\n"
                ret += "        <span>{{scope.row." + column_name + " | quantityFilter | thousands}}</span>\n"
                ret += "    </template>\n"
                ret += "</el-table-column>\n"
            elif data_type == 'NVARCHAR2' or data_type == 'VARCHAR2':
                ret += '<!-- ' + comments + '  -->\n' + "<el-table-column align='left' prop='" + column_name + "' :label=\"$t('" + comments + "')\" width='100' show-overflow-tooltip></el-table-column>\n"
            # elif data_type == 'NVARCHAR2':

            # ret += column_name + ': \'' + column_name + '\',' + data_type + '\n'
        print(ret)
        self.save_data(ret)

    def do2(self):
        ret = ''
        for f in self.arrs:
            column_name = f[0]
            data_type = f[1]
            comments = f[2]

            ret += "{label: '" + comments + "',width: '200',prop: '" + column_name + "'},\n"

        print(ret)
        self.save_data(ret)

    # 3、数据存储
    def save_data(self, dataInfo):
        # 1、打开txt文件     w:覆盖以前的内容；a:在最后追加
        file_handle = open('data.txt', mode='w')
        # 2、向文件写入数据
        file_handle.write(dataInfo + ' \n')
        # 3、关闭文件
        file_handle.close()
        # pass


if __name__ == "__main__":
    os.chdir(os.path.abspath(os.path.dirname(__file__)))
    db = db_class()
    db.tableName = 'ZM_TBL_QLTY_PUB_INSPECTION'
    db.db_data()
    db.str2Hump()
    db.do1()
    # db.do2()
    # db.save_data(res)
